package Eyjafjalla.action;

import Eyjafjalla.helper.EyjafjallaHelper;
import Eyjafjalla.helper.StringHelper;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.MakeTempCardInHandAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.UIStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;

import java.util.ArrayList;

public class CloudCakeAction extends AbstractGameAction {
    public CloudCakeAction(AbstractMonster enemy, int amt){
        this.amount = amt;
        this.startDuration = duration = Settings.ACTION_DUR_FAST;
        actionType = ActionType.CARD_MANIPULATION;
        this.enemy = enemy;
    }

    AbstractMonster enemy;

    private CardGroup tmp = new CardGroup(CardGroup.CardGroupType.UNSPECIFIED);

    private static final UIStrings uiStrings;

    private boolean hasSelect = false;

    @Override
    public void update() {
        if(startDuration==duration){
            if(this.amount<=0){
                this.isDone = true;
                return;
            }
            tmp.group = EyjafjallaHelper.getCakeCards(this.amount);
            AbstractDungeon.gridSelectScreen.open(tmp,1,false,uiStrings.TEXT[5]);
        }
        else if(!hasSelect){
            hasSelect = true;
            ArrayList<AbstractCard> cardsToEnemy = new ArrayList<>();
            ArrayList<AbstractCard> cardsToPlayer = new ArrayList<>();
            for(AbstractCard c:tmp.group){
                if(AbstractDungeon.gridSelectScreen.selectedCards.contains(c)){
                    cardsToEnemy.add(0,c);
                }
                else {
                    cardsToPlayer.add(0,c);
                }
            }
            for(AbstractCard c:cardsToPlayer){
                EyjafjallaHelper.setCakePowerToCreature(c,AbstractDungeon.player);
            }
            for(AbstractCard c:cardsToEnemy){
                EyjafjallaHelper.setCakePowerToCreature(c,this.enemy);
            }
            AbstractDungeon.gridSelectScreen.selectedCards.clear();
        }


        this.tickDuration();
    }

    static {
        uiStrings = CardCrawlGame.languagePack.getUIString(StringHelper.CLOUD_OPERATION);
    }
}

